<?php
/*
<!-- HTML FOR TESTING THE POST COMMAND -->
<html>
	<form action="getfriends.php" method="post">
	UserId: <input type="text" name="id" />
	<input type="submit" />Users
	</form>
</html>
*/
?>
<?php

	function checkResult($result){
		if(!$result){
	    		die($message);
		}
	}

	if ($_SERVER['REQUEST_METHOD'] == 'GET') {
	
		mysql_connect("localhost","akosciak","MojaPtaszek");
		
		mysql_select_db("songbump") or die( "Unable to select database");

		$query = "SELECT Name, Id From Users";
		$result = mysql_query($query);
		checkResult($result);
		
		// User Results
		$graph = array();
		while ($row = mysql_fetch_assoc($result)) {

			$adjacencies = array();

			$query = "SELECT End From Edges WHERE Start=".$row["Id"];
			$resultB = mysql_query($query);
			checkResult($resultB);

			while ($rowB = mysql_fetch_assoc($resultB)){
				$adjacencies[] = $rowB["End"];
			}
			
			$node = array(
				"adjacencies" => $adjacencies,
				"id" => $row["Id"],
				"name" => $row["Name"].":".$row["Id"]
			);
	
			$graph[] = $node;		
		}
		
		mysql_free_result($result);
			
		$graphJSON = json_encode($graph);
		echo $graphJSON;
		
	}
	
?>
